package com.community.core.entity;


import com.community.listener.BaseEntityListener;
import com.es.core.utils.FastJSONUtil;
import com.es.core.utils.JacksonJsonDateSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;

import java.io.Serializable;
import java.util.Date;

/**
 * 统一定义ID的entity基类
 * Created by zhanglj
 * Date 2015/11/24.
 */
@MappedSuperclass
@EntityListeners(value={BaseEntityListener.class})
@DynamicInsert
@DynamicUpdate
public class BaseEntity implements Serializable{
    /**
     * 实体主键
     */
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    protected Long id;
    /**
     * 实体版本
     */
    @Version
    protected Long version;

    /**
     * 创建时间
     */
    protected Date createTime;

    /**
     * 创建�?
     */
    protected String createBy;

    /**
     * �?��修改时间
     */
    protected Date lastModifyTime;

    /**
     * �?��修改�?
     */
    protected String lastModifyBy;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }
    @JsonSerialize(using=JacksonJsonDateSerializer.class)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }
    @JsonSerialize(using=JacksonJsonDateSerializer.class)
    public Date getLastModifyTime() {
        return lastModifyTime;
    }

    public void setLastModifyTime(Date lastModifyTime) {
        this.lastModifyTime = lastModifyTime;
    }

    public String getLastModifyBy() {
        return lastModifyBy;
    }

    public void setLastModifyBy(String lastModifyBy) {
        this.lastModifyBy = lastModifyBy;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result=prime*result+((id==null)?0:id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        BaseEntity other = (BaseEntity) obj;
        if(id==null){
            if(other.id!=null)
                return false;
        }else if(!id.equals(other.id))
            return false;
        return true;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return FastJSONUtil.toJsonString(this);
    }
}
